package com.animal.controller;

import com.animal.common.Result;
import com.animal.entity.pojo.ABlog;
import com.animal.entity.pojo.R;
import com.animal.entity.pojo.Reply;
import com.animal.entity.vo.ABlogVo;
import com.animal.mapper.ReplyMapper;
import com.animal.service.ABlogService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发布博客
 */
@RestController
@RequestMapping("/blog")
public class AblogController {

    @Autowired
    private ABlogService blogService;

    @Autowired
    private ReplyMapper replyMapper;

    /**
     *
     * 发布博客
     */
    @PostMapping("/publish")
    public R publish (@RequestBody ABlog blog){
        if (blogService.addBlog(blog)){
            return R.success("发布成功！");
        }
        return R.error("发布失败");
    }

    /**
     * 查看博客评论
     * @param BlogId
     * @return
     */
    @PostMapping("/findAllByRemarkId/{BlogId}")
    public R findAllByRemarkId(@PathVariable Long BlogId){
        List<Reply> allByRemarkId = replyMapper.findAllByRemarkId(BlogId);
        return R.success(allByRemarkId);
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        boolean delete = blogService.delete(id);
        if(delete){
            replyMapper.delete(id);
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        boolean flag = blogService.deleteByIds(ids);
        if (flag) {
            replyMapper.deleteByIds(ids);
            return Result.success();
        }else {
            return Result.error();
        }
    }
}
